<?php

/*
 * This class is used for as Model for Language table 
 */

class Model_Language extends Model_Base {

    protected $_name = 'tbl_language';
    protected $_primaryKey = 'lang_id';

    /**
     *  This function is used to get Language
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function language_fetchall() {
        $select = $this->select()
                ->from(array('con' => 'tbl_language'))
                ->where('lang_status != 2')
                ->order('lang_name asc');
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function getlanguageArray() {
        // $languageModel = new Model_Language();
        $languageArray = $this->language_fetchall();
        $finalArray[''] = '-- Select Language --';
        foreach ($languageArray as $com) {
            $finalArray[$com['lang_id']] = $com['lang_name'];
        }
        return $finalArray;
    }

    public function getStatusArray() {
        return array(
            '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('prd' => 'tbl_language'))
                ->where('lang_id = ?', $id)
                ->where('lang_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get language
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchType($where = "", $limit = "", $offset = "0", $order = 'lang_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('lang' => 'tbl_language'))
                ->where('lang_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total rows
     * @return int
     */
    public function getTypeCount($where) {
        $select = $this->select()
                ->from(array('lang' => 'tbl_language'), Array('cnt' => 'count(*)'))
                ->where('lang_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete language
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteType($prdtArray) {
        $loginModel = new Model_Login();
        if (is_array($prdtArray)) {

            foreach ($prdtArray as $_company) {
                $companyData = Array();
                $companyData = $this->fetchEntryById($_company);
                if (count($companyData) > 0) {
                    $data = array('lang_status' => '2');
                    $this->update($data, 'lang_id =' . (int) $_company);
                }
            }
        } else {

            $lang_id = $prdtArray;
            $companyData = $this->fetchEntryById($lang_id);
            if (count($companyData) > 0) {
                $data = array('lang_status' => '2');
                $this->update($data, 'lang_id =' . $lang_id);
            }
        }
    }

    /*     * ******Update status 1 if language is default*************** */

    public function updatedefaultlang($lang_id) {
        $data1 = array('lang_isdefault' => '0');
        $this->update($data1, 'lang_isdefault =1');
        $data = array('lang_isdefault' => '1');
        $this->update($data, 'lang_id =' . (int) $lang_id);
    }

}
